//
//  ProfileViewOtherAvatarCell.swift
//  iModern
//
//  Created by Ralf on 5/14/15.
//  Copyright (c) 2015 com.starlooking. All rights reserved.
//

import UIKit

class ProfileViewOtherAvatarCell: TableViewCell
{
    var contact: NSDictionary!

    let __background_image_view_tag = 10
    let __avatar_image_view_tag     = 11

    let __alias_label_tag     = 20
    let __about_me_label_tag  = 21
    let __signature_label_tag = 22
    let __follow_label_tag    = 23
    let __name_label_tag      = 44
    let __follow_button_tag   = 30

    override func awakeFromNib()
    {
        super.awakeFromNib()

        initBackgroundImageView()
        initAvatarImageView()
        initAliasLabel()
        initFollowButton()
//        initAboutMeLabel()
        initSignatureLabel()
        initNameLabel()
    }

    override func setSelected(selected: Bool, animated: Bool)
    {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    func setData(data: NSDictionary)
    {
        //var rs: ResultSet?
        self.contact = data
        //var ivAvatar = self.viewWithTag(__avatar_image_view_tag) as! UIImageView
        let imageURL = data["shopLogoURL"] as? String
        if (imageURL != nil)
        {
            let url      = appDelegate.parameterManager.server + "/" + imageURL!
            let ivAvatar = self.viewWithTag(__avatar_image_view_tag) as! UIImageView
            ivAvatar.load(url, placeholder: nil)
        }

        let backgroundURL = data["backgroundURL"] as? String

        if (backgroundURL != nil)
        {
            let ivBackground = self.viewWithTag(__background_image_view_tag) as! UIImageView
            ivBackground.load(appDelegate.parameterManager.server + "/" + backgroundURL!, placeholder: nil)
        }

        let lAlias = self.viewWithTag(__alias_label_tag) as! UILabel
        lAlias.text = contact["shopName"] as? String

        let label = self.viewWithTag(__signature_label_tag) as! UILabel
        label.text = contact["address"] as? String

        let labelName = self.viewWithTag(__name_label_tag) as! UILabel
        let fullName  = contact["fullName"] as? String
        let city      = contact["city"] as? String
        labelName.text = fullName! + "       " + city!

        let button = self.viewWithTag(__follow_button_tag) as! UIButton
        button.addTarget(self, action: Selector("SendMessage"), forControlEvents: UIControlEvents.TouchUpInside)


//  alias
//        var lAboutMe = self.viewWithTag(__about_me_label_tag) as! UILabel
//
//        var constellation = data["constellation"] as? String
//        var blood         = data["blood"] as? String
//
//        var aboutMe = ""
//
//        if constellation != nil
//        {
//            aboutMe = constellation!
//        }
//
//        if blood != nil
//        {
//            aboutMe += "/" + blood!
//        }
//
//        if count(aboutMe) > 2
//        {
//            lAboutMe.text = aboutMe
//        }
//
//        var signature = data["signature"] as? String
//        if signature != nil
//        {
//            if count(signature!) > 0
//            {
//                var lSignature = self.viewWithTag(__signature_label_tag) as! UILabel
//                lSignature.text = signature
//            }
//        }
//
//        var button = self.viewWithTag(__follow_button_tag) as? UIButton
//        button!.tag = __follow_button_tag
//        let userName = contact["userName"] as! String
//
//        button!.selected = false
//
//        if appDelegate.isLogged()
//        {
//            rs = appDelegate.contactManager.isFollowing(userName) as ResultSet
//
//            if (rs == nil)
//            {
//                self.alertNetworkConnectionException()
//                return
//            }
//
//            if (rs!.hasErrors())
//            {
//                self.alertWarningMessage(rs?.errorMessage)
//                return
//            }
//
//            if (!rs!.isEmpty())
//            {
//                let follow = rs?.toDictionary()
//                if (follow?.count > 0)
//                {
//                    button!.selected = true
//                }
//            }
//        }
    }

    func initBackgroundImageView()
    {
        var imageView = self.viewWithTag(__background_image_view_tag) as? UIImageView
        if (imageView == nil)
        {
            let frame = CGRect(x: 0, y: 0, width: screen.size.width, height: 240)
            imageView = UIImageView(frame: frame)
            imageView!.tag = __background_image_view_tag
            imageView!.image = UIImage(named: "Avatar-Backgound.png")
            self.addSubview(imageView!)
        }
    }

    func initAvatarImageView()
    {
        var imageView = self.viewWithTag(__avatar_image_view_tag) as? UIImageView
        if (imageView == nil)
        {
            let x     = (screen.size.width - 100) / 2
            let frame = CGRect(x: x, y: 30, width: 100, height: 100)
            imageView = UIImageView(frame: frame)
            imageView!.image = UIImage(named: "Avatar-Default.png")
            imageView!.tag = __avatar_image_view_tag
            imageView!.layer.cornerRadius = 50
            imageView!.layer.masksToBounds = true
            imageView!.layer.borderColor = UIColor.blackColor().CGColor
            imageView!.layer.borderWidth = 1
            self.addSubview(imageView!)
        }
    }

    func initAliasLabel()
    {
        var label = self.viewWithTag(__alias_label_tag) as? UILabel
        if (label == nil)
        {
            let frame = CGRect(x: 0, y: 130, width: screen.width, height: 40)
            label = UILabel(frame: frame)
            label!.textAlignment = .Center
            label!.tag = __alias_label_tag
            label!.font = UIFont.systemFontOfSize(20)
            label!.textColor = UIColor.whiteColor()
            label!.shadowColor = UIColor.darkGrayColor()
            label!.shadowOffset = CGSizeMake(1, 1)
            self.addSubview(label!)
        }
    }

    func initFollowButton()
    {

        var button = self.viewWithTag(__follow_button_tag) as? UIButton
        if (button == nil)
        {
            let x     = (screen.size.width - 200) / 2
            let frame = CGRect(x: x + 180, y: 160, width: 30, height: 30)
            button = UIButton(frame: frame)
            button!.contentMode = .Left
            button!.titleLabel?.font = UIFont.systemFontOfSize(12)
            button!.titleLabel!.shadowColor = UIColor.darkGrayColor()
            button!.titleLabel!.shadowOffset = CGSizeMake(1, 1)
            button!.tag = __follow_button_tag
            button!.imageView?.contentMode = .ScaleAspectFit
            button!.setImage(UIImage(named: "Message-60.png"), forState: .Normal)
            self.addSubview(button!)
        }
    }

    func initNameLabel()
    {
        var labelName = self.viewWithTag(__name_label_tag) as? UILabel
        if (labelName == nil)
        {
            let x     = (screen.size.width - 200) / 2
            let frame = CGRect(x: x, y: 160, width: 180, height: 30)
            labelName = UILabel(frame: frame)
            labelName!.textAlignment = .Center
            labelName!.tag = __name_label_tag
            labelName!.font = UIFont.systemFontOfSize(12)
            labelName!.textColor = UIColor.whiteColor()
            labelName!.shadowColor = UIColor.darkGrayColor()
            labelName!.shadowOffset = CGSizeMake(1, 1)
            labelName!.text = "匠人名称       哪里人"
            self.addSubview(labelName!)
        }

    }

    func initAboutMeLabel()
    {
        var label = self.viewWithTag(__about_me_label_tag) as? UILabel
        if (label == nil)
        {
            let frame = CGRect(x: 0, y: 190, width: screen.width, height: 20)
            label = UILabel(frame: frame)
            label!.textAlignment = .Center
            label!.tag = __about_me_label_tag
            label!.font = UIFont.systemFontOfSize(12)
            label!.textColor = UIColor.whiteColor()
            label!.shadowColor = UIColor.darkGrayColor()
            label!.shadowOffset = CGSizeMake(1, 1)
            label!.text = "销量：100"
            self.addSubview(label!)
        }
    }

    func initSignatureLabel()
    {
        var label = self.viewWithTag(__signature_label_tag) as? UILabel
        if (label == nil)
        {
            let frame = CGRect(x: 0, y: 210, width: screen.width, height: 20)
            label = UILabel(frame: frame)
            label!.textAlignment = .Center
            label!.tag = __signature_label_tag
            label!.font = UIFont.systemFontOfSize(12)
            label!.textColor = UIColor.whiteColor()
            label!.shadowColor = UIColor.darkGrayColor()
            label!.shadowOffset = CGSizeMake(1, 1)
            label!.text = "匠铺地址：北京市朝阳区默默大厦120层1203"
            self.addSubview(label!)
        }
    }

    func SendMessage()
    {
        let controller = ChatViewController()
        controller.contact = self.contact as! [String:AnyObject]
        self.pushViewController(controller)
    }
}
